package rayCaster;

import static helper.Util.*;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

public class Ray {
	// Un rayo tiene ecuacion: r = origin + k*(dir-origin), con k >= 0
	private Point3d origin;
	private Vector3d vecDir;
	
	public Ray(Point3d origin, Point3d dir) {
		this.origin = origin;
		this.vecDir = getVector(origin, dir);
	}
	
	public Ray(Point3d origin, Vector3d vecDir) {
		this.origin = origin;
		this.vecDir = vecDir;
	}
	
	public Point3d getOrigin() {
		return origin;
	}
	
	public Vector3d getVecDir() {
		return vecDir;
	}
	
	// Dado un k >=0 me devuelve un punto del rayo
	public Point3d getPoint(double k) throws Exception {
		// p = origin + k*DirRayo, k >= 0
		if( k < 0 )
			throw new Exception("Ray:getPoint: k debe ser positivo o cero");		
		Point3d p = new Point3d();
		p.scale(k, vecDir);
		p.add(origin);
		return p;
	}
	
	// Dado un k >=0 me devuelve un punto del rayo en p	
	public void getPoint(double k, Point3d p) throws Exception {
		// p = origin + k*DirRayo, k >= 0
		if( k < 0 )
			throw new Exception("Ray:getPoint: k debe ser positivo o cero");
		p.scale(k, vecDir);
		p.add(origin);
	}
	
	public String toString(){
		return "Ray info = \n\tOrigin = " + origin + "\n\tVector = " + vecDir;
	}
	
}
